package cn.wolfcode.luowowo.article.service.impl;


import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.domain.TravelContent;
import cn.wolfcode.luowowo.article.mapper.TravelContentMapper;
import cn.wolfcode.luowowo.article.mapper.TravelMapper;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.ITravelService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class TravelServiceImpl implements ITravelService {
    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private TravelContentMapper travelContentMapper;

    @Override
    public PageInfo query(TravelQuery qo) {
        //然后调用查询类的排序字段
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        return new PageInfo(travelMapper.selectForList(qo));
    }

    @Override
    public Long saveOrUpdate(Travel travel) {
        //摘要处理
        String ct = travel.getTravelContent().getContent();
        if (ct.length() > 200) {
            travel.setSummary(ct.substring(0, 200));
        } else {
            travel.setSummary(ct);
        }
        //最后更新时间
        travel.setLastUpdateTime(new Date());
        if (travel.getId() == null) {
            //创建时间
            travel.setCreateTime(new Date());
            travelMapper.insert(travel);
            TravelContent travelContent = travel.getTravelContent();
            travelContent.setId(travel.getId());
            travelContentMapper.insert(travelContent);
        } else {
            travelMapper.updateByPrimaryKey(travel);
            TravelContent travelContent = travel.getTravelContent();
            travelContent.setId(travel.getId());
            travelContentMapper.updateByPrimaryKey(travelContent);
        }
        return travel.getId();
    }

    @Override
    public Travel get(Long id) {
        return travelMapper.selectByPrimaryKey(id);
    }

    @Override
    public TravelContent getContent(Long id) {
        return travelContentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Travel> queryViewNumTop3(Long destId) {
        return travelMapper.selectViewNumTop3(destId);
    }

    @Override
    public List<Travel> list() {
        return travelMapper.selectAll();
    }

}

